package com.foreknow.demo8;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Set接口
 * 特点： 不能有重复的元素，无序的集合
 * 实现类： HashSet  TreeSet(具有排序功能)
 */
public class SetDemo {

    public void getInfo(Set<Person> set) {
        //遍历
        Iterator<Person> it = set.iterator();
        while (it.hasNext()){
            Person person = it.next();
            System.out.println(person.getName());
        }
    }
    public static void main(String[] args) {

        Person p1 = new Person(1,"tom",20);
        Person p2= new Person(2,"fender",21);
        Person p3 = new Person(3,"gibson",22);

        //创建Set集合
        Set<Person> set = new HashSet<>();
        //向集合中添加元素
        set.add(p1);
        set.add(p2);
        set.add(p3);

        SetDemo setDemo = new SetDemo();
        setDemo.getInfo(set);
    }
}
